﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section header{

}

<body>
    <input type="hidden" id="isReload" value="0">
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header">
                        <button class="layui-btn" data-type="Insert">新增任务</button>
                        @*<button class="layui-btn" data-type="Initialize">重新初始化</button>*@
                    </div>
                    <div class="layui-card-body ">
                        <table class="layui-table" lay-data="{id: 'taskTable'}" lay-filter="demo">
                            <thead>
                                <tr>
                                    <th lay-data="{type:'checkbox'}"></th>
                                    <th lay-data="{field:'id', width:80}">ID</th>
                                    <th lay-data="{field:'name', minWidth:100}">名称</th>
                                    <th lay-data="{width:100, templet: '#jobTypeTemplet'}">任务类型</th>
                                    <th lay-data="{width:100, templet: '#switchStatus'}">状态</th>
                                    <th lay-data="{width:120, templet: '#runStatusTemplet' }">运行状态</th>
                                    <th lay-data="{field:'cronExpression', width:120}">cron表达示</th>
                                    <th lay-data="{field:'runTime', width:200}">上次运行时间</th>
                                    <th lay-data="{field:'runEndTime', width:200}">上次运行结束时间</th>
                                    <th lay-data="{field:'runValue', width:200}">最后执行的值</th>
                                    <th lay-data="{field:'jobFullName', width:200}">任务完整地址</th>
                                    <th lay-data="{field:'runTimeoutSeconds', width:120}">最大超时秒数</th>
                                    <th lay-data="{field:'remark', minWidth:150}">备注</th>
                                    <th lay-data="{fixed: 'right', width:600, align:'center', toolbar: '#barDemo'}">操作</th>
                                </tr>
                            </thead>
                        </table>

                        <script type="text/html" id="barDemo">
                            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="StartJob">载入任务</a>
                            <a class="layui-btn layui-btn-xs" lay-event="TriggerJob">立即执行</a>
                            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="Interrupt">强制中止任务</a>
                            <a class="layui-btn layui-btn-xs" lay-event="UpdateTaskInfoByLastValue">修改最后执行的值</a>
                            <a class="layui-btn layui-btn-xs" lay-event="UpdateTask">编辑</a>
                            <a class="layui-btn layui-btn-xs" lay-event="QueryLogs">日志</a>
                        </script>

                        <script type="text/html" id="jobTypeTemplet">
                            {{# if( d.jobType == 1) { }}
                            <span>一般任务</span>
                            {{# } else { }}
                            <span>API任务</span>
                            {{# } }}
                        </script>

                        <script type="text/html" id="switchStatus">
                            <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|暂停" lay-filter="templetStatus"
                                   {{ d.status == 1 ? 'checked' : '' }}>
                        </script>

                        <script type="text/html" id="runStatusTemplet">
                            {{# if( d.runStatus == 1) { }}
                            <span>未运行</span>
                            {{# } else { }}
                            <span style="color:red">运行中</span>
                            {{# } }}
                        </script>
                    </div>

                </div>
            </div>
        </div>
    </div>
</body>

<script>
    layui.use(['table', 'laypage'], function () {
        var table = layui.table;
        var form = layui.form;

        table.reload("taskTable", {
            height: "full-70",
            // width: 'full-20',
            loading: true,
            page: false,
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.statusCode, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "data": res.data //解析数据列表
                };
            }
        });

        function tableReload() {
            table.reload("taskTable", {
                url: "/Schedule/GetTasks",
            });
        }

        tableReload();

        form.on('switch(templetStatus)', function (obj) {
            var _this = this;
            var promptLayer = common.reloadMsg("正在切换，请稍等");
            var taskId = this.value;
            var url = "";
            if (this.checked == false) {
                url = "/Schedule/PauseJob";
            }
            else {
                url = "/Schedule/ResumeJob";
            }
            request.ajax({
                type: "post",
                url: url,
                data: { id: taskId },
                success: function (data) {
                    layer.close(promptLayer);
                    if (data.success) {
                        common.successMsg('切换成功！');
                    }
                    else {
                        common.errorMsg(data.message);
                        _this.checked = !_this.checked;
                        form.render();
                    }

                }
            });
        });
        table.on('tool(demo)', function (obj) {
            var data = obj.data;

            if (obj.event == "StartJob") { //载入任务
                onStartJob(data);
            }
            else if (obj.event === 'TriggerJob') { //立即执行
                onTriggerJob(data);
            }
            else if (obj.event === "Interrupt") {
                onInterrupt(data);
            }
            else if (obj.event === 'UpdateTaskInfoByLastValue') { //修改最后执行的值
                onUpdateTaskInfoByLastValue(data);
            }
            else if (obj.event == "UpdateTask") //编辑任务
            {
                onUpdateTask(data);
            }
            else if (obj.event === "QueryLogs") {
                onQueryLogs(data);
            }
        });

        //载入任务
        function onStartJob(data) {
            layer.confirm('确定重新载入任务?', function (index) {
                var promptLayer = common.reloadMsg("正在载入，请稍等");
                request.ajax({
                    type: "post",
                    url: "/Schedule/StartJob",
                    data: { id: data.id },
                    success: function (data) {
                        layer.close(promptLayer);
                        if (data.success) {
                            common.successMsg('操作成功');
                        }
                        else {
                            common.errorMsg(data.message);
                        }
                    }
                });
            });
        }

        //立即执行
        function onTriggerJob(data) {
            layer.confirm('确定立即执行?', function (index) {
                var promptLayer = common.reloadMsg("正在立即执行，请稍等");
                request.ajax({
                    type: "post",
                    url: "/Schedule/TriggerJob",
                    data: { id: data.id },
                    success: function (data) {
                        layer.close(promptLayer);
                        if (data.success) {
                            common.successMsg('已立即执行');
                        }
                        else {
                            common.errorMsg(data.message);
                        }
                    }
                });
            });
        }

        function onInterrupt(data) {
            layer.confirm('确定强制中止任务吗?', function (index) {
                var promptLayer = common.reloadMsg("正在中止任务，请稍等");
                request.ajax({
                    type: "post",
                    url: "/Schedule/Interrupt",
                    data: { id: data.id },
                    success: function (data) {
                        layer.close(promptLayer);
                        if (data.success) {
                            common.successMsg('成功中止');
                            tableReload();
                        }
                        else {
                            common.errorMsg(data.message);
                        }
                    }
                });
            });
        }
        //修改最后执行的值
        function onUpdateTaskInfoByLastValue(data) {
            $("#isReload").val("0")
            common.open("修改最后执行的值", "500px", "200px", "/Schedule/UpdateRunValue?taskId=" + data.id, 0, function () {
                if ($("#isReload").val() == "1") {
                    tableReload();
                }
            });
        }
        //编辑任务
        function onUpdateTask(data) {
            $("#isReload").val("0")
            common.open("修改计划任务", "800px", "700px", "/Schedule/Update?taskId=" + data.id, 0, function () {
                if ($("#isReload").val() == "1") {
                    tableReload();
                }
            });
        }


        var active = {
            Insert: function () {
                $("#isReload").val("0")
                common.open("新增角色", "800px", "700px", common.versionUrl("/Schedule/Insert"), 0, function () {
                    if ($("#isReload").val() == "1") {
                        tableReload();
                    }
                });
            },
            Initialize: function () {
                layer.confirm('确定重新初始化吗?', function (index) {
                    var promptLayer = common.reloadMsg("正在初始化，请稍等");
                    request.ajax({
                        type: "post",
                        url: "/Schedule/Initialize",
                        success: function (data) {
                            layer.close(promptLayer);
                            if (data.success) {
                                common.successMsg('操作成功');
                            }
                            else {
                                common.errorMsg(data.message);
                            }
                        }
                    });
                });
            }
        };

        $('.layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //任务日志
        function onQueryLogs(data) {
            common.open("任务日志", "1200px", "700px", "/Schedule/TaskLogs?taskId=" + data.id, 0, function () {
            });
        }
    });
</script>